<template>
	<el-container v-loading="loading">
		<el-container>
			<el-header>
				<div class="left-panel">
					<el-button type="primary" v-auth="'user:user:edit'" icon="el-icon-plus" @click="handleData('add')"></el-button>
					<el-button @click="importUser">导入用户
					</el-button>
					<el-button icon="el-icon-download" @click="downloadTpl">下载导入模板
					</el-button>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.username" placeholder="员工姓名" clearable></el-input>
						<el-input v-model="search.mobile" placeholder="手机号" clearable></el-input>
						<el-select v-model="search.company_id" placeholder="单位名称" style="min-width:25%;" clearable>
							<el-option v-for="item in companyList" :key="item.company_id" :label="item.company_name" :value="item.company_id">
							</el-option>
						</el-select>
						<el-select v-model="search.status" placeholder="账号状态" style="min-width:19%;" clearable>
							<el-option v-for="item in statusList" :key="item.status" :label="item.status_name" :value="item.status">
							</el-option>
						</el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="resetsearch">重置</el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort
						 remoteFilter>
					<el-table-column type="selection" width="50"></el-table-column>
					<el-table-column label="ID" prop="user_id" width="100"></el-table-column>
					<el-table-column label="姓名" prop="username" width="150"></el-table-column>
					<el-table-column label="性别" prop="sex_name" width="150"></el-table-column>
					<el-table-column label="手机号" prop="mobile" width="150"></el-table-column>
					<el-table-column label="单位名称" prop="company_name" width="200"></el-table-column>
					<el-table-column label="账号状态" prop="status" width="80">
						<template #default="scope">
							<el-tag v-if="scope.row.status==1" type="success">启用</el-tag>
							<el-tag v-if="scope.row.status==0" type="danger">停用</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="是否注销" prop="is_close" width="80">
						<template #default="scope">
							<el-tag v-if="scope.row.is_close==1" type="danger">是</el-tag>
							<el-tag v-if="scope.row.is_close==0" type="success">否</el-tag>
						</template>
					</el-table-column>
					<el-table-column label="创建时间" prop="created_at" width="170"></el-table-column>
					<el-table-column label="创建人" prop="created_by" width="170"></el-table-column>
					<el-table-column label="启用停用" prop="status" width="80">
						<template #default="scope">
							<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)"
									   :loading="scope.row.$switch_status" active-value="1" inactive-value="0"></el-switch>
						</template>
					</el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="160">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small"
										   @click="handleData('show',scope.row, scope.$index)">查看
								</el-button>
								<el-button text type="primary" v-auth="'user:user:edit'" size="small"
										   @click="handleData('edit',scope.row, scope.$index)">编辑
								</el-button>
								<el-button v-if="scope.row.status==0" text type="primary" v-auth="'user:user:delete'" size="small"
										   @click="deleteUser('delete',scope.row, scope.$index)">删除
								</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save.vue'

export default {
	name: 'user',
	components: {
		saveDialog
	},
	data() {
		return {
			dialog: {
				save: false
			},
			showGroupLoading: false,
			loading: false,
			apiObj: this.$API['user.user'].index,
			selection: [],
			statusList: [
				{status: 0, status_name: "停用"},
				{status: 1, status_name: "启用"},
			],
			companyList: [],
			search: {
				username: null,
				mobile: null,
				status: null,
				company_id: null,
			}
		}
	},
	mounted() {
		//获取单位列表
		this.getCompanyList()
	},
	methods: {
		getCompanyList() {
			this.$API['company.company'].getCompanyList.get().then((res) => {
				if (res.code == 0) {
					this.companyList = res.data;
				}
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		handleData(mode, row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open(mode).setData(row)
			})
		},
		//删除
		deleteUser(mode, row) {
			console.log(mode, row);
			this.$confirm(`注：确认删除此用户吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: 'el-button--danger'
			}).then(() => {
				this.$API['user.user'].deleteUser.post({user_id: row.user_id})
					.then((res) => {
						if (res.code == 0) {
							this.$refs.table.refresh()
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					})
					.catch((msg) => {
						this.$alert(msg, "提示", {type: 'error'})
					})
					.finally(() => {

					});
			}).catch(() => {
			})

		},
		downloadTpl() {
			this.$API['common'].getImportTemplate.get({file_type: 1}).then((res) => {
				window.open(res.data.jump_url);
			}).catch((msg) => {
				this.$alert(msg, "提示", {type: 'error'})
			})
		},
		importUser() {
			this.$router.push({
				path: '/user/user_import',
				query: {}
			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//表格内开关
		changeSwitch(val, row) {
			row.$switch_status = true;
			this.$API['user.user'].changeStatus.post({status: val, user_id: row.user_id})
				.then((res) => {
					if (res.code == 0) {
						this.$emit('success', this.form, this.mode)
						this.$message.success("操作成功")
						row.status = val;
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				})
				.catch((msg) => {
					this.$alert(msg, "提示", {type: 'error'})
				})
				.finally(() => {
					row.$switch_status = false;
				});
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//重置
		resetsearch() {
			this.search = {
				username: null,
				mobile: null,
				status: null,
				company_id: null,
			}
		},
		//本地更新数据
		handleSuccess() {
			this.dialog.save = false
			this.$refs.table.reload()
		},
	}
}
</script>

<style>
</style>
